import { Controller } from "../../mvc/core/Controller";
import { IView } from "../../mvc/interfaces/IView";
import { ActiveDelegateModule } from "../../../modules/ActiveDelegateModule";
import { GModuleEvent } from "../mvc/GModuleEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { ServerCommand } from "../mvc/ServerCommand";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { DataEvent } from "../events/DataEvent";
import { GameProxy } from "../mvc/GameProxy";
import { ActiveDelegateIcon } from "../view/activeDelegate/ActiveDelegateIcon";
import { ActiveDelegateConfig } from "../resource/ActiveDelegateConfig";
import { DelegateTaskData } from "../view/delegate/cell/DelegateTaskData";
import { DelegateTaskStatus } from "../view/delegate/DelegateTaskStatus";
import { TSweepUp } from "../../../Message/Db/Tables/TSweepUp";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { PulseSharedObject } from "../view/player/PulseSharedObject";
import { TodayNoTipsConst } from "../../component/gconst/TodayNoTipsConst";
import { CopyConfig } from "../resource/CopyConfig";
import { Alert } from "../../../com/mui/controls/Alert";
import { CheckBoxWin } from "../view/common/alertwins/CheckBoxWin";
import { DataProvider } from "../../../fl/data/DataProvider";
import { IActiveDelegateModule } from "../../../modules/interfaces/IActiveDelegateModule";
type int = number;
//class ActiveDelegateController
    
    export  class ActiveDelegateController extends Controller
    {
       

        protected /* override */ initView(): IView
        {
            if (this._activeDelegateModule == null) 
            {
                this._activeDelegateModule = new ActiveDelegateModule();
                this._activeDelegateModule.addEventListener(GModuleEvent.StartDelegateTaskFree, this.onStartDelegate);
                this._activeDelegateModule.addEventListener(GModuleEvent.ShowNextDelegateTask, this.onShowNextDelegateTask);
                NetDispatcher.addCmdListener(ServerCommand.BufferUpdate, this.onRoleStatusUpdate);
                NetDispatcher.addCmdListener(ServerCommand.VIPInfoUpdateCommand, this.onRoleStatusUpdate);
                RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleStatusUpdate);
            }
            return this._activeDelegateModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.AcitveTimeUpdate_SweepUp, this.onSweepUpActiveChange);
            NetDispatcher.addCmdListener(ServerCommand.ActiveDelegateCanSweepUpList, this.onReceiveCanSweepUpList);
            NetDispatcher.addCmdListener(ServerCommand.DelegateWarehouseItemsUpdate, this.onDelegateWarehouseItemsUpdate);
            return;
        }

        /* internal  */onSweepUpActiveChange(arg1: DataEvent): void
        {
            var loc1=arg1.data;
            if (loc1) 
            {
                this.showIcon();
                GameProxy.delegateTask.canSweepUp();
            }
            else 
            {
                this.hideIcon();
            }
            return;
        }

        /* internal  */showIcon(): void
        {
            this.icon.show();
            return;
        }

        /* internal  */hideIcon(): void
        {
            if (this._activeDelegateIcon && !this._activeDelegateIcon.isHide) 
            {
                this._activeDelegateIcon.hide();
            }
            return;
        }

        public get icon(): ActiveDelegateIcon
        {
            if (this._activeDelegateIcon == null) 
            {
                this._activeDelegateIcon = new ActiveDelegateIcon();
                this._activeDelegateIcon.addEventListener(flash.events.MouseEvent.CLICK, this.onIconClick);
            }
            return this._activeDelegateIcon;
        }

        /* internal  */onIconClick(arg1: Object /* flash.events.MouseEvent */): void
        {
            view.show();
            this.onDelegateWarehouseItemsUpdate();
            this._tempDelegateTaskCode = 0;
            GameProxy.delegateTask.canSweepUp();
            return;
        }

        /* internal  */onReceiveCanSweepUpList(arg1: Array<any>): void
        {
            var loc3=null;
            this.updateDelegateTasks();
            var loc1=false;
            var loc2=ActiveDelegateConfig.instance.canActiveDelegateList;
            var loc4=0;
            while (loc4 < loc2.length) 
            {
                if ((loc3 = loc2.getItemAt(loc4) as DelegateTaskData).status == DelegateTaskStatus.CanDelegate) 
                {
                    loc1 = true;
                    break;
                }
                ++loc4;
            }
            if (!loc1) 
            {
                this.hideIcon();
            }
            return;
        }

        /* internal  */updateDelegateTasks(): void
        {
            var loc2=null;
            var loc4=false;
            var loc1=ActiveDelegateConfig.instance.canActiveDelegateList;
            var loc3=0;
            while (loc3 < loc1.length) 
            {
                loc2 = loc1.getItemAt(loc3) as DelegateTaskData;
                if (loc4 = cache.activeDelegate.isCanSweep(loc2.code)) 
                {
                    loc2.tsweepUp = cache.activeDelegate.getSweepUpByCode(loc2.code);
                }
                if (cache.copy.isCopyNumOut(loc2.code)) 
                {
                    loc2.status = DelegateTaskStatus.Complete;
                }
                else if (cache.role.roleInfo.level < Math.min(loc2.copy.enterMinLevel, loc2.tsweepUp.levelLimit)) 
                {
                    loc2.status = DelegateTaskStatus.UnReach;
                }
                else if (loc4) 
                {
                    loc2.status = DelegateTaskStatus.CanDelegate;
                }
                else 
                {
                    loc2.status = DelegateTaskStatus.CanNotDelegate;
                }
                ++loc3;
            }
            loc1.sortOn(["status", "id"], [Array.NUMERIC, Array.NUMERIC]);
            if (this._activeDelegateModule && !this._activeDelegateModule.isHide) 
            {
                this._activeDelegateModule.updateDelegateTask(loc1);
                if (this.showToNext()) 
                {
                    this._tempDelegateTaskCode = 0;
                    this.onShowNextDelegateTask();
                }
            }
            return;
        }

        /* internal  */showToNext(): Boolean
        {
            if (this._tempDelegateTaskCode == 0 || !this._activeDelegateModule) 
            {
                return false;
            }
            var loc1=this._activeDelegateModule.selectedDelegate;
            if (loc1 && loc1.code == this._tempDelegateTaskCode && !(loc1.status == DelegateTaskStatus.CanDelegate)) 
            {
                return true;
            }
            return false;
        }

        /* internal  */onStartDelegate(arg1: DataEvent): void
        {
            var loc5=0;
            var loc6=0;
            var loc7=0;
            var loc8=null;
            var loc9=null;
            var loc1=arg1.data.data as TSweepUp;
            var loc2=arg1.data.count as int;
            var loc3=arg1.data.double;
            var loc4=cache.role.money.gold;
            if (loc3 && loc1.gold > loc4) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69624));
                return;
            }
            if (PulseSharedObject.isTodayNotTips(TodayNoTipsConst.ActiveDelegateTip)) 
            {
                this.startDelegate(loc1, loc3, loc2);
            }
            else 
            {
                loc5 = loc1.gold * loc2;
                loc6 = loc1.experience;
                loc7 = cache.delegateTask.getExpAddVIP() + cache.delegateTask.getExpAddDrug();
                loc6 = Math.round(loc6 + loc6 * loc7 / 100) * loc2;
                loc9 = CopyConfig.instance.getInfoByCode(loc1.code);
                if (loc3) 
                {
                    loc8 = Language.getStringByParam(69716, loc5, loc9.name);
                }
                else 
                {
                    loc8 = Language.getStringByParam(69715, loc9.name);
                }
                Alert.alertWinRenderer = CheckBoxWin;
                Alert.extendObj = {"info": loc1, "costGold": loc3, "count": loc2};
                Alert.show(loc8, Language.getString(69900), Alert.OK | Alert.CANCEL, null, this.onActiveDelegateAlertHandler);
            }
            return;
        }

        /* internal  */onActiveDelegateAlertHandler(arg1: int, arg2: Object, arg3: int): void
        {
            if (arg1 == Alert.OK) 
            {
                this.startDelegate(arg2.info, arg2.costGold, arg2.count);
            }
            if (arg3) 
            {
                PulseSharedObject.save(TodayNoTipsConst.ActiveDelegateTip, true);
            }
            return;
        }

        /* internal  */startDelegate(arg1: TSweepUp, arg2: Boolean, arg3: int): void
        {
            this._tempDelegateTaskCode = arg1.code;
            GameProxy.delegateTask.sweepUp(arg1.id, arg2, arg3);
            return;
        }

        /* internal  */onShowNextDelegateTask(arg1=null): void
        {
            var loc1=arg1 ? true : false;
            var loc2=this.getNextDelegateTask(loc1);
            if (loc2) 
            {
                if (this._activeDelegateModule && !this._activeDelegateModule.isHide) 
                {
                    this._activeDelegateModule.setSelectedDelegateData(loc2);
                }
            }
            return;
        }

        /* internal  */onRoleStatusUpdate(arg1): void
        {
            if (this._activeDelegateModule && !this._activeDelegateModule.isHide) 
            {
                this.updateDelegateTasks();
            }
            return;
        }

        /* internal  */getNextDelegateTask(arg1: Boolean=false): DelegateTaskData
        {
            var loc4=null;
            var loc5=0;
            var loc6=0;
            var loc1=this._activeDelegateModule.selectedDelegate;
            var loc2=ActiveDelegateConfig.instance.canActiveDelegateList;
            var loc3=loc2.getItemIndex(loc1);
            if (!arg1) 
            {
                loc5 = 0;
                while (loc5 < loc2.length) 
                {
                    if ((loc6 = loc3 + loc5 + 1) >= loc2.length) 
                    {
                        loc6 = loc6 - loc2.length;
                    }
                    if ((loc4 = loc2.getItemAt(loc6) as DelegateTaskData) && loc4.status == DelegateTaskStatus.CanDelegate) 
                    {
                        return loc4;
                    }
                    ++loc5;
                }
            }
            if (arg1) 
            {
                if ((loc6 = loc3 + 1) >= loc2.length) 
                {
                    loc6 = loc6 - loc2.length;
                }
                if (loc4 = loc2.getItemAt(loc6) as DelegateTaskData) 
                {
                    return loc4;
                }
            }
            return null;
        }

        /* internal  */onDelegateWarehouseItemsUpdate(arg1=null): void
        {
            var loc1=false;
            if (this._activeDelegateModule && !this._activeDelegateModule.isHide) 
            {
                loc1 = cache.pack.delegate.itemLength > 0;
                if (loc1) 
                {
                    this._activeDelegateModule.addWarehouseBtnEffect();
                }
                else 
                {
                    this._activeDelegateModule.removeWarehouseBtnEffect();
                }
            }
            return;
        }

        private /* var */_canDelegateListDp: DataProvider;

        private /* var */_activeDelegateIcon: ActiveDelegateIcon;

        private /* var */_activeDelegateModule: IActiveDelegateModule;

        private /* var */_tempDelegateTaskCode: int;
    }
